package client.layer;

import java.util.regex.*;

public class InputValidator {
    private static final Pattern emailP = Pattern.compile("[a-zA-Z]{1}\\w{1,50}+@([a-zA-Z]){1,}\\.((net)|(com)|(org)|(ru))");
    private static final Pattern dateP = Pattern.compile("(\\d{2}\\.){2}\\d{4}");
    private static final Pattern nameP = Pattern.compile("^[A-Z]{1}[a-z]{1,}$");
    private static final int MONTH_NUMBER = 12;
    private static final int DAYS_NUMBER = 31;

    public static boolean emailCheckValid(String value) {
        Matcher m = emailP.matcher(value);
        if (!m.matches()) {
            MessageBox.ok("You have entered incorrect email");
        }
        return m.matches();
    }

    public static boolean dateCheckValid(String value) {
        Matcher m = dateP.matcher(value);
        int month = 0;
        int day = 0;
        if (m.matches()) {
            String[] dd = value.split("\\.");
            day = (int) Integer.parseInt(dd[0]);
            month = (int) Integer.parseInt(dd[1]);
        }
        if (!m.matches() && month <= MONTH_NUMBER && day <= DAYS_NUMBER) {
            MessageBox.ok("You have entered incorrect date");
        }
        return m.matches();
    }

    public static boolean nameCheckValid(String value) {
        Matcher m = nameP.matcher(value);
        if (!m.matches()) {
            MessageBox.ok("You have entered incorrect Name");
        }
        return m.matches();
    }
}
